package com.zxbg.service.impl.systemtoolsServiceImpl;

import com.zxbg.dao.AppCategoryDao;
import com.zxbg.domain.AppCategory;
import com.zxbg.domain.Internationalization;
import com.zxbg.service.systemtoolsService.WorkflowTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class WorkflowTypeServiceImpl implements WorkflowTypeService {
    @Autowired
    AppCategoryDao appCategoryDao;

    @Override
    public boolean deleteCategory(Integer category_id) {
        return appCategoryDao.deleteCategory(category_id) > 0;
    }

    @Override
    public boolean updateCategory(AppCategory appCategory) {
        return appCategoryDao.updateCategory(appCategory) > 0;
    }

    @Override
    public boolean addCategory(AppCategory appCategory, Integer tenant_id) {
        appCategory.setCategory_name_zh(appCategory.getCategory_name().getZh());
        appCategory.setCategory_name_en(appCategory.getCategory_name().getEn());
        appCategory.setAdd_time(new Timestamp(System.currentTimeMillis()));
        appCategory.setTenant_id(tenant_id);
        return appCategoryDao.addCategory(appCategory) > 0;
    }

    @Override
    public boolean changeOrder(Integer oldIndex, Integer newIndex, Integer tenant_id) {
        List<AppCategory> appCategoryList = getAllCategory(tenant_id);
        AppCategory oldApp = appCategoryList.get(oldIndex);
        AppCategory newApp = appCategoryList.get(newIndex);
        appCategoryDao.updateCategoryOrderByOrder(newApp.getOrder(), oldApp.getOrder(), tenant_id);
        appCategoryDao.updateCategoryOrderByCategoryId(oldApp.getCategory_id(), newApp.getOrder());
        return true;
    }

    @Override
    public List<AppCategory> getAllCategory(Integer tenant_id) {
        List<AppCategory> res = appCategoryDao.getAllCategory(tenant_id);
        for (int i = 0; i < res.size(); i++) {
            AppCategory temp = res.get(i);
            Internationalization internationalization = new Internationalization(temp.getCategory_name_zh(), temp.getCategory_name_en());
            temp.setCategory_name(internationalization);
        }
        return res;
    }
}
